#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>

using namespace std;

static void setR4(unsigned char *p, float *r)
{
    memcpy(p, r, 4);
}

struct TSR_0_ASignalType
{
    uint8_t TSR_ID_1 : 8;
    uint8_t TSR_Sign_Name_1 : 8;
    uint8_t TSR_Sign_Name_2 : 2;
    uint8_t TSR_Confidence : 6;
    uint8_t TSR_Confidence_1 : 1;
    uint8_t TSR_Relevancy : 5;
    uint8_t TSR_Filter_Type : 2;
    uint8_t TSR_Filter_Type_1 : 2;
    uint8_t TSR_Sup1_SignName_1 : 6;
    uint8_t TSR_Sup1_SignName_2 : 2;
    uint8_t TSR_Sup1_Confidence : 6;
    uint8_t TSR_Sup1_Confidence_1 : 1;
    uint8_t : 7;
    uint8_t : 0;
    uint8_t : 4;
    uint8_t Rolling_Counter : 4;
};

struct BS
{
    int b : 2;
    uint64_t a : 2;
    // uint8_t c : 1;
};

void test01()
{
    const unsigned char data[] = {0x00, 0xFF, 0x03, 0x00, 0x00, 0x00, 0x00, 0x10};
    cout << sizeof(TSR_0_ASignalType) << endl;
    cout << sizeof(BS) << endl;
    cout << sizeof(data) << endl;
    TSR_0_ASignalType test;
    memcpy(&test, data, sizeof(test));
    cout <<"********************" <<endl;
    cout << int(test.TSR_ID_1) << endl;
    cout << int(test.TSR_Sign_Name_1) << endl;
    cout << int(test.TSR_Sign_Name_2) << endl;
    cout << int(test.TSR_Relevancy) << endl;
    cout << int(test.TSR_Filter_Type) << endl;
    cout << int(test.TSR_Filter_Type_1) << endl;
    cout << test.TSR_Sup1_Confidence << endl;
    cout << test.TSR_Sup1_Confidence_1 << endl;

}

struct TEST
{
    unsigned int a : 5;
    unsigned int b : 7;
    unsigned int c : 5;
};

void test02()
{   
    /* 

    结果分析:内存拷贝函数只拷贝了4个字符到mytest中，mytest中的三个变量加起来才15位，其它17位我们不要管。那么这15位中的值是怎么样的呢？
    15位还没到2个字节，所以这15位中全在He中，H的AscII码为72(01001000)，e的AscII 码为112(01100101)，根据小端法，
    我们知道他们的存储方式为：01100101 01001000,a占前5位：01000即8;b占接下来的7位，0101010即42；c占接下来的3位，110即-2（因为这里是有符号数，高位为1，表示负数，所以是-2）。

    如果把他们全变成unsigned int，如：
    struct TEST{
        unsigned int a:5;
        unsigned int b:7;
        unsigned int c:3;
    };
    那么输出就是：8,42,6。 

    */


    TEST mytest;
    cout << sizeof(mytest) <<endl;
    memcpy(&mytest, "HelloWorld", sizeof(mytest));
    cout << mytest.a << endl;
    cout << mytest.b << endl;
    cout << mytest.c << endl;
}

int main()
{
    // unsigned char p[4] = {0};
    // float r = 3.1415927;
    // setR4(p, &r);
    // for (int i = 0; i < sizeof(p) / sizeof(p[0]); i++)
    // {
    //     printf("%X ", p[i]);
    // }
    // // cout << p << endl;
    // // getchar();


    test02();
    // test02();
    return 1;
}